<template>
  <div>
    <el-drawer
      @close="handleClose"
      destroy-on-close
      title="数据授权"
      :visible="show"
      direction="rtl"
      ref="drawer"
      close-on-press-escape
      size="30%"
    >
      <div>
        <div class="j-drawer__content">
          <el-form
            :model="drawerForm"
            :rules="drawerRules"
            ref="drawerForm"
            :inline="true"
            class="b-form"
            label-width="80px"
            status-icon
          >
            <el-form-item label="权限类别" prop="type">
              <el-select
                v-model="drawerForm.type"
                filterable
                placeholder="请选择权限类别"
                @change="changeSelect"
              >
                <el-option
                  v-for="item in types"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="权限选择">
              <el-tree
                show-checkbox
                check-strictly
                :data="treeDatas"
                :props="defaultProps"
                default-expand-all
                v-loading="treeLoading"
                node-key="id"
                ref="tree"
                empty-text=""
              >
              </el-tree>
            </el-form-item>
          </el-form>
        </div>
        <div class="j-drawer__footer">
          <el-button
            type="primary"
            @click="clickSubmitForm('drawerForm')"
            size="small"
            >确定</el-button
          >
          <el-button @click="handleClose" size="small">取消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listTreeDepartments } from "@/api/system/department";

import {
  insertRolePermission,
  updateRolePermission,
  listRolePermissions,
} from "@/api/system/role-permission.js";

export default {
  name: "DataAuthPicker",
  props: {
    show: {
      type: Boolean,
      required: true,
    },
    data: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      drawerForm: {
        id: "",
        type: "",
        roleId: this.data,
        permission: "",
      },
      drawerRules: {
        type: [{ required: true, message: "请选择权限类别", trigger: "blur" }],
      },
      types: [
        { id: 0, name: "全部数据权限" },
        { id: 1, name: "本部门数据权限" },
        { id: 2, name: "本部门及以下数据权限" },
        { id: 3, name: "本人数据权限" },
        { id: 4, name: "自定义数据权限" },
      ],
      treeDatas: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      treeLoading: false,
    };
  },
  created() {
    const t = this;
    t.loadRolePermissions();
  },
  methods: {
    // 查询角色数据权限
    loadRolePermissions() {
      const t = this;
      listRolePermissions({ roleId: t.data, field: "id", order: "asc" })
        .then((response) => {
          if (response.data.length === 1) {
            t.drawerForm = response.data[0];
            t.$refs.tree.setCheckedKeys(t.drawerForm.permission.split(","));
            if (t.drawerForm.type === 4) {
              t.loadTree();
            }
          }
        })
        .catch(() => {});
    },
    // 查询树
    loadTree() {
      const t = this;
      t.treeLoading = true;
      setTimeout(() => {
        listTreeDepartments()
          .then((response) => {
            t.treeLoading = false;
            t.treeDatas = response.data;
          })
          .catch(() => {
            t.treeLoading = false;
          });
      }, 500);
    },
    // 选择组件值切换事件
    changeSelect(value) {
      const t = this;
      if (value === 4) {
        t.loadTree();
      } else {
        t.treeDatas = [];
      }
    },
    // 保存
    clickSubmitForm(formName) {
      const t = this;
      const treeKeys = this.$refs.tree.getCheckedKeys();
      const type = t.drawerForm.type;
      if (type === 4 && treeKeys.length === 0) {
        t.$notify({
          title: "失败",
          message: "请选择权限",
          type: "",
          position: "bottom-left",
          duration: 2000,
        });
        return;
      }

      t.drawerForm.permission = treeKeys.join(",");
      t.$refs[formName].validate((valid) => {
        if (valid) {
          if (t.drawerForm.id === "") {
            // 新增
            insertRolePermission(t.drawerForm)
              .then((response) => {
                t.$notify({
                  title: "成功",
                  message: response.message,
                  type: "success",
                  position: "bottom-left",
                  duration: 2000,
                });
                t.handleClose();
              })
              .catch(() => {});
          } else {
            // 编辑
            updateRolePermission(t.drawerForm)
              .then((response) => {
                t.$notify({
                  title: "成功",
                  message: response.message,
                  type: "success",
                  position: "bottom-left",
                  duration: 2000,
                });
                t.handleClose();
              })
              .catch(() => {});
          }
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.j-drawer__content {
  padding: 0px 24px;
  margin-bottom: 56px;
  overflow: auto;
}

.j-drawer__footer {
  display: flex;
  height: 56px;
  width: 100%;
  align-items: center;
  position: absolute;
  bottom: 0px;
  box-sizing: border-box;
  justify-content: flex-end;
  border-top: 1px solid #f0f0f0;
  padding-right: 24px;
  background-color: #fff;
  z-index: 999;
}
</style>
